package com.chunyu.ask.controller.baike;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chunyu.ask.bean.Article;
import com.chunyu.ask.bean.Channel;
import com.chunyu.ask.bean.Tag;
import com.chunyu.ask.controller.BaseController;
import com.chunyu.ask.service.ArticleService;
import com.chunyu.ask.service.ChannelService;
import com.chunyu.ask.util.PageUtils;
import com.chunyu.ask.util.RespMsg;

/**
 * 描        述：健康百科
 * 创建时间：2016-8-24
 * @author Jibaole
 */
@Controller
@RequestMapping("/baike")
public class BaikeController extends BaseController {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ChannelService channelService;
	/**
	 * 健康百科-首页
	 * @param urlName
	 * @param channelStrId
	 * @return
	 */
	@RequestMapping("/")
	public @ResponseBody ModelAndView index() {
		ModelAndView view = new ModelAndView(PageUtils.JKBK_LINK);
		Map<String, Object> paramMap = new HashMap<String, Object>();
			try {
				paramMap.put("firstResult", 0);
				paramMap.put("maxResult", PageUtils.MAX_PAGE);
				paramMap.put("parentId", PageUtils.CHANNEL_ID_JKBK);
				paramMap.put("isValid", 1);//有效
				paramMap.put("sortId", 1);//排序
				StringBuffer sbf = new StringBuffer("");
				List<Channel> channelList= channelService.getList(paramMap);
				if(!CollectionUtils.isEmpty(channelList)){
					for(int i=0;i < channelList.size();i++){
						Channel channelObj = channelList.get(i);
						sbf.append(channelObj.getId());
						if(i < channelList.size()-1){
							sbf.append(",");
						}
					}
				}
				if(!StringUtils.isEmpty(sbf)){
					paramMap.clear();
					paramMap.put("firstResult", 0);
					paramMap.put("sortType", 1);//时间倒序
					paramMap.put("channelIds", sbf.toString());
					
					List<Article> baikeList = articleService.getList(paramMap);
					int max=6;
			        int min=2;
			        Random random = new Random();
			        int s = random.nextInt(max)%(max-min+1) + min;
			        
					view.addObject("channelId",s);//随机产生一个频道id
					view.addObject("baikeList",baikeList);
				}
			} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	
	@RequestMapping("/{channelStrId}/")
	public @ResponseBody ModelAndView Jkjy(@PathVariable String channelStrId) {
		ModelAndView view = new ModelAndView(PageUtils.JKBK_LINK);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		int id=0;
			try {
			paramMap.put("strId", channelStrId);
			paramMap.put("parentId", PageUtils.CHANNEL_ID_JKBK);
			//频道id
			Channel obj=channelService.getChannel(paramMap);
			
			if(obj != null){
				id = obj.getId();
				view.addObject("channelId",id);
				view.addObject("tagName",obj.getName());
			}
		
			//对应咨询文章
			paramMap.clear();
			paramMap.put("channelId", id);
			List<Article> baikeList = articleService.getList(paramMap);
			view.addObject("baikeList",baikeList);
			} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	
	 /**
		 * 获取：健康百科tag标签列表
		 * @return
		 */
		@RequestMapping("/getTagList")
		public @ResponseBody RespMsg getTagList() {
			RespMsg result = new RespMsg();
			List<Tag> tagList = new ArrayList<Tag>();
			try {
				tagList.add(new Tag("/baike/nvxing/", "女性专栏"));
				tagList.add(new Tag("/baike/muying/", "母婴育儿"));
				tagList.add(new Tag("/baike/yangsheng/", "养生保健"));
				tagList.add(new Tag("/baike/shenghuo/", "健康生活"));
				result.resultObj=tagList;
				result.status = true;
			} catch (Exception e) {
				result.status = false;
				result.msg="加载百科频道列表异常";
				e.printStackTrace();
			}
			return result;
		}
}
